package cn.tedu.antconlonysever.contract.controller;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.JsonResult;
import cn.tedu.antconlonysever.contract.pojo.param.ContractAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.ContractUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractListVo;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractStandardVO;
import cn.tedu.antconlonysever.contract.service.IContractService;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/contract")
@Api(tags = "01.合同管理")
@Slf4j
@Validated
public class ContractController {
    @Autowired
    private IContractService service;

    // http://localhost:8080/contract/add-new
    @PostMapping("/add-New")
    @ApiOperation("添加合同")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid ContractAddNewParam contractAddNewParam) {
        log.debug("开始处理【添加合同】的请求，参数：{}", contractAddNewParam);
        service.addNew(contractAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除合同")
    @PreAuthorize("hasAuthority('/pms/contract/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "根据ID删除合同失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID删除合同】的请求，参数：{}", id);
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改合同")
    @PreAuthorize("hasAuthority('/pms/contract/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的合同ID值!") Long id,
                                           @Valid ContractUpdateParam contractUpdateParam) {
        log.debug("开始处理【修改合同】的请求，新数据：{}", contractUpdateParam);
        service.updateInfoById(id, contractUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询合同详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同id", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "根据id查询合同详情失败！请输入合法的数据参数，") Long id) {
        log.debug("开始处理【根据ID查询合同详情】的请求，iD：{}，", id);
        ContractStandardVO result = service.getStandardById(id);
        return JsonResult.ok(result);

    }


    @GetMapping("")
    @ApiOperation("查询合同列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<ContractListVo>> list(@Range(min = 1, message = "查询合同列表失败，请提供正确的页码值！") Integer page,
                                                     String queryType) {
        log.debug("开始处理【查询合同列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<ContractListVo> pageData;
        if ("all".equals(queryType)) {
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = service.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }
}
